Show code
pacman::p_load(tidyverse, kableExtra, knitr,
ggthemes, RColorBrewer, tm, plotly, highcharter,
ggridges, ggdist, ggpubr
)Teo Suan Ern
March 18, 2024
March 31, 2024
In section will cover Aspatial Analysis module of the R Shiny Application.
The project uses p_load() of pacman package to check if the R packages are installed in the computer.
tidyverse: a family of modern R packages specially designed to support data science, analysis and communication task including creating static statistical graphs.
knitr: an report generation tool.
ggthemes: an R package that provides extra themes, geoms and scales to ggplot2 package.
DT: an R interface to the JavaScript library DataTables that create interactive table on html page.
plotly: an R package for creating interactive charts.
tm: an R package that provides text mining applications.
ggforce: an extension of ggplot2 to provide visual data analysis with newer stats and geoms.
The project will examine the dataset from Armed Conflict Location & Event Data Project (ACLED), specifically Myanmar country, between Year 2010 and Year 2023.
The dataset consists of 55,574 observations and 35 variables. Each row details the armed conflict event on the type, agents, location, date and other characteristics of conflict events (such as political violence, demonstration) in Myanmar.
str() to check the structure of the data.'data.frame': 55574 obs. of 35 variables:
$ event_id_cnty : chr "MMR56099" "MMR56222" "MMR56370" "MMR56376" ...
$ event_date : chr "31-Dec-23" "31-Dec-23" "31-Dec-23" "31-Dec-23" ...
$ year : int 2023 2023 2023 2023 2023 2023 2023 2023 2023 2023 ...
$ time_precision : int 1 1 1 1 1 1 1 1 1 1 ...
$ disorder_type : chr "Political violence" "Political violence" "Political violence" "Demonstrations" ...
$ event_type : chr "Explosions/Remote violence" "Explosions/Remote violence" "Battles" "Protests" ...
$ sub_event_type : chr "Shelling/artillery/missile attack" "Shelling/artillery/missile attack" "Armed clash" "Peaceful protest" ...
$ actor1 : chr "Military Forces of Myanmar (2021-)" "Military Forces of Myanmar (2021-)" "Phoenix DF: Phoenix Defense Force (Nattalin)" "Protesters (Myanmar)" ...
$ assoc_actor_1 : chr "" "" "" "" ...
$ inter1 : int 1 1 3 6 1 1 3 1 2 1 ...
$ actor2 : chr "" "Civilians (Myanmar)" "Military Forces of Myanmar (2021-)" "" ...
$ assoc_actor_2 : chr "" "" "" "" ...
$ inter2 : int 0 7 1 0 7 0 1 0 1 7 ...
$ interaction : int 10 17 13 60 17 10 13 10 12 17 ...
$ civilian_targeting: chr "" "Civilian targeting" "" "" ...
$ iso : int 104 104 104 104 104 104 104 104 104 104 ...
$ region : chr "Southeast Asia" "Southeast Asia" "Southeast Asia" "Southeast Asia" ...
$ country : chr "Myanmar" "Myanmar" "Myanmar" "Myanmar" ...
$ admin1 : chr "Mon" "Rakhine" "Bago-West" "Sagaing" ...
$ admin2 : chr "Mawlamyine" "Maungdaw" "Thayarwady" "Yinmarbin" ...
$ admin3 : chr "Ye" "Maungdaw" "Nattalin" "Salingyi" ...
$ location : chr "Aing Shey" "Kaing Gyi (NaTaLa)" "Kyauk Pyoke" "Let Pa Taung" ...
$ latitude : num 15.3 20.7 18.6 22.1 18.6 ...
$ longitude : num 98 92.4 95.8 95.1 95.8 ...
$ geo_precision : int 1 2 2 2 1 1 1 2 2 1 ...
$ source : chr "Democratic Voice of Burma" "Development Media Group; Narinjara News" "Khit Thit Media; Myanmar Pressphoto Agency" "Myanmar Labour News" ...
$ source_scale : chr "National" "Subnational" "National" "National" ...
$ notes : chr "On 31 December 2023, in Aing Shey village (Ye township, Mawlamyine district, Mon state), following a clash betw"| __truncated__ "On 31 December 2023, in Kaing Gyi (Mro) village (coded as Kaing Gyi (NaTaLa)) (Maungdaw township, Maungdaw dist"| __truncated__ "On 31 December 2023, near Kyauk Pyoke village (Nattalin township, Thayarwady district, Bago-West region), the P"| __truncated__ "On 31 December 2023, in the Let Pa Taung area of Salingyi township (Yinmarbin district, Sagaing region), protes"| __truncated__ ...
$ fatalities : int 0 0 4 0 0 0 3 0 0 0 ...
$ tags : chr "" "" "" "crowd size=no report" ...
$ timestamp : int 1704831212 1704831213 1704831214 1704831214 1704831214 1704831216 1704831216 1704831216 1704831216 1704831216 ...
$ population_1km : int NA NA NA 749 NA 178 6634 671 687 35292 ...
$ population_2km : int NA NA NA 521 NA 135 19078 2197 654 85732 ...
$ population_5km : int 3081 NA NA 1358 NA NA 34396 3144 656 169473 ...
$ population_best : int 3081 NA NA 749 NA NA 34396 3144 656 85732 ...
The output above reveals that event_date is in character format instead of date format.
colSums to check for missing valuesThe output below shows that there are four variables with missing values.
duplicate() to check for duplicates:There are no duplicate entries in the dataset.
[1] event_id_cnty event_date year time_precision
[5] disorder_type event_type sub_event_type actor1
[9] assoc_actor_1 inter1 actor2 assoc_actor_2
[13] inter2 interaction civilian_targeting iso
[17] region country admin1 admin2
[21] admin3 location latitude longitude
[25] geo_precision source source_scale notes
[29] fatalities tags timestamp population_1km
[33] population_2km population_5km population_best
<0 rows> (or 0-length row.names)
The flowchart diagram below provides an overview of the key variables used in this project.
flowchart TD A(Key Variables Used \n event_id_cnty) A --> B(Time Period) A --> C(Characteristic of Incident) A --> D(Location) B --> E(year) B --> F(date) C --> G(event_type) C --> H(sub_event_type) C --> J(fatalities) C -.-> K(New Variables) K -.-> L(total incidents) K -.-> M(total fatalities) D --> N(country) D --> O(longitude) D --> P(latitude) D --> Q(admin1) D --> R(admin2) D -.-> S(New Variables) S -.-> T(geometry points) S -.-> U(shapeID)
The code chunk below uses dmy() to convert the date format from character to date format:
The code chunk below creates the following new variables based on total armed conflict incidents and total fatalities (by disorder_type and sub_event_type):
Annual percentage of political violence (admin1 and admin2)
Annual percentage of violence against civilian (admin1 and admin2)
data_admin1 <- data %>%
group_by(year, admin1) %>%
filter(year != 2024) %>%
mutate(
total_fata = sum(fatalities),
total_inci = n(),
## fatalities
# Political violence
political_count = round(
sum(total_fata[event_type %in% c("Battles", "Protests",
"Explosions/Remote violence",
"Violence against civilians")]) /
sum(total_fata)),
# Violence against civilian
civilian_count = round(
sum(total_fata[event_type == "Violence against civilians"]) /
sum(total_fata))
) %>%
ungroup()data_admin2 <- data %>%
group_by(year, admin2) %>%
filter(year != 2024) %>%
mutate(
total_fata = sum(fatalities),
total_inci = n(),
## fatalities
# Political violence
political_count = round(
sum(total_fata[event_type %in% c("Battles", "Protests",
"Explosions/Remote violence",
"Violence against civilians")]) /
sum(total_fata)),
# Violence against civilian
civilian_count = round(
sum(total_fata[event_type == "Violence against civilians"]) /
sum(total_fata))
) %>%
ungroup()The code chunk below selects/ excludes the variables intended to be used for this project. Cleaned dataset is named as final.
This newly created dataset will be used for line chart and density ridge plot.
The following two newly created datasets will be used for aspatial analysis of Administrative Region 1 and 2 respectively.
final_a1 <- data_admin1 %>%
select(-time_precision, -geo_precision,
-source_scale, -timestamp, -tags,
-population_1km, -population_2km, -population_5km, -population_best)
final_a2 <- data_admin2 %>%
select(-time_precision, -geo_precision,
-source_scale, -timestamp, -tags,
-population_1km, -population_2km, -population_5km, -population_best)The code chunk below save the cleaned dataset in .rds format for subsequent geospatial EDA and R Shiny Application.
Use str() to check the structure of the final dataset.
tibble [55,574 × 26] (S3: tbl_df/tbl/data.frame)
$ event_id_cnty : chr [1:55574] "MMR56099" "MMR56222" "MMR56370" "MMR56376" ...
$ event_date : Date[1:55574], format: "2023-12-31" "2023-12-31" ...
$ year : int [1:55574] 2023 2023 2023 2023 2023 2023 2023 2023 2023 2023 ...
$ disorder_type : chr [1:55574] "Political violence" "Political violence" "Political violence" "Demonstrations" ...
$ event_type : chr [1:55574] "Explosions/Remote violence" "Explosions/Remote violence" "Battles" "Protests" ...
$ sub_event_type : chr [1:55574] "Shelling/artillery/missile attack" "Shelling/artillery/missile attack" "Armed clash" "Peaceful protest" ...
$ actor1 : chr [1:55574] "Military Forces of Myanmar (2021-)" "Military Forces of Myanmar (2021-)" "Phoenix DF: Phoenix Defense Force (Nattalin)" "Protesters (Myanmar)" ...
$ assoc_actor_1 : chr [1:55574] "" "" "" "" ...
$ inter1 : int [1:55574] 1 1 3 6 1 1 3 1 2 1 ...
$ actor2 : chr [1:55574] "" "Civilians (Myanmar)" "Military Forces of Myanmar (2021-)" "" ...
$ assoc_actor_2 : chr [1:55574] "" "" "" "" ...
$ inter2 : int [1:55574] 0 7 1 0 7 0 1 0 1 7 ...
$ interaction : int [1:55574] 10 17 13 60 17 10 13 10 12 17 ...
$ civilian_targeting: chr [1:55574] "" "Civilian targeting" "" "" ...
$ iso : int [1:55574] 104 104 104 104 104 104 104 104 104 104 ...
$ region : chr [1:55574] "Southeast Asia" "Southeast Asia" "Southeast Asia" "Southeast Asia" ...
$ country : chr [1:55574] "Myanmar" "Myanmar" "Myanmar" "Myanmar" ...
$ admin1 : chr [1:55574] "Mon" "Rakhine" "Bago-West" "Sagaing" ...
$ admin2 : chr [1:55574] "Mawlamyine" "Maungdaw" "Thayarwady" "Yinmarbin" ...
$ admin3 : chr [1:55574] "Ye" "Maungdaw" "Nattalin" "Salingyi" ...
$ location : chr [1:55574] "Aing Shey" "Kaing Gyi (NaTaLa)" "Kyauk Pyoke" "Let Pa Taung" ...
$ latitude : num [1:55574] 15.3 20.7 18.6 22.1 18.6 ...
$ longitude : num [1:55574] 98 92.4 95.8 95.1 95.8 ...
$ source : chr [1:55574] "Democratic Voice of Burma" "Development Media Group; Narinjara News" "Khit Thit Media; Myanmar Pressphoto Agency" "Myanmar Labour News" ...
$ notes : chr [1:55574] "On 31 December 2023, in Aing Shey village (Ye township, Mawlamyine district, Mon state), following a clash betw"| __truncated__ "On 31 December 2023, in Kaing Gyi (Mro) village (coded as Kaing Gyi (NaTaLa)) (Maungdaw township, Maungdaw dist"| __truncated__ "On 31 December 2023, near Kyauk Pyoke village (Nattalin township, Thayarwady district, Bago-West region), the P"| __truncated__ "On 31 December 2023, in the Let Pa Taung area of Salingyi township (Yinmarbin district, Sagaing region), protes"| __truncated__ ...
$ fatalities : int [1:55574] 0 0 4 0 0 0 3 0 0 0 ...
Before proceeding with data visualisation, it is essential to be able to navigate the dataset of 55,574 observations and 32 variables with ease. This segment will help users identify or navigate through the dataset observations instead of scrolling through each observation one-by-one. The interactive datatable is created using DT package.
Display number of observations by selecting the dropdown (5, 10, 25, 50, 100 entries). This ensure that the observations will not span across the entire webpage.
View other pages of observations with “previous” or “next” button.
Search specific observations with the search bar for the occurrence of a string/ numerical value in any column of an observation
Filter observations with the filter bar directly below column headers.
Column visibility allows user to select the columns that they are interested to view and hide the rest.
DT::datatable(
final,
class = "compact",
filter = "top",
extensions = c("Buttons"),
options = list(
pageLength = 5,
columnDefs = list(
list(targets = c(1:7, 9, 11:23, 26), className = "dt-center"),
list(targets = c(8, 10, 24, 25), visible = FALSE)),
buttons = list(
list(extend = c("colvis"), columns = c(1:26))),
dom = "Bpiltf"),
caption = "Table 1: Armed Conflicts in Myanmar (2010-2023)"
)